﻿//#define TEST_API
//#define NO_SDK

using Cysharp.Threading.Tasks;

#if TEST_API || !UNITY_EDITOR
#if UNITY_WEBGL
#if NO_SDK
#elif WEIXINMINIGAME
#elif BYTEDANCEMINIGAME
using TTSDK;
#elif QGMINIGAME
using QGMiniGame;
using UnityEngine;
#else
#endif
#elif UNITY_ANDROID
#if NO_SDK
#elif GOOGLE_PLAY
#else
#endif
#else
#endif
#else
#endif

namespace Runtime.SDK {

    public static partial class DFR_API {

        public static async UniTask<bool> CheckShortcut() {
#if TEST_API || !UNITY_EDITOR
#if UNITY_WEBGL
#if NO_SDK
            await UniTask.Yield();
            return false;
#elif WEIXINMINIGAME
            await UniTask.Yield();
            return false;
#elif BYTEDANCEMINIGAME
            if (CanIUse.CheckShortcut) {
                var isDone = false;
                var result = false;
                TT.CheckShortcut(isExsit => {
                    result = isExsit;
                    isDone = true;
                });
                await KHBD.Context.WaitUntil(() => isDone);
                return result;
            } else {
                await UniTask.Yield();
                return false;
            }
#elif QGMINIGAME
            bool result = false;
            QG.HasShortcutInstalled(
                (msg) => {
                    Debug.Log("已经添加到桌面 " + JsonUtility.ToJson(msg));
                    result = true;
                },
                (msg) => {
                    Debug.Log("尚未添加到桌面 " + msg.errMsg);
                    result = false;
                });
            await UniTask.Yield();
            return result;
#else
#endif
#elif UNITY_ANDROID
#if NO_SDK
            await UniTask.Yield();
            return false;
#elif GOOGLE_PLAY
            await UniTask.Yield();
            return false;
#else
#endif
#else
            await UniTask.Yield();
            return true;
#endif
#else
            await UniTask.Yield();
            return false;
#endif
        }

        public static async UniTask<bool> AddShortcut() {
#if TEST_API || !UNITY_EDITOR
#if UNITY_WEBGL
#if NO_SDK
            await UniTask.Yield();
            return false;
#elif WEIXINMINIGAME
            await UniTask.Yield();
            return false;
#elif BYTEDANCEMINIGAME
            if (CanIUse.AddShortcut) {
                var isDone = false;
                var result = false;
                TT.AddShortcut(isSuccessed => {
                    result = isSuccessed;
                    isDone = true;
                });
                await KHBD.Context.WaitUntil(() => isDone);
                return result;
            } else {
                await UniTask.Yield();
                return false;
            }
#elif QGMINIGAME
            bool result = false;
            QG.InstallShortcut(
                "",
                (msg) => {
                    Debug.Log("创建成功 " + JsonUtility.ToJson(msg));
                    result = true;
                },
                (msg) => {
                    Debug.Log("创建失败 " + msg.errMsg);
                    result = false;
                });
            await UniTask.Yield();
            return result;
#else
#endif
#elif UNITY_ANDROID
#if NO_SDK
            await UniTask.Yield();
            return false;
#elif GOOGLE_PLAY
            await UniTask.Yield();
            return false;
#else
#endif
#else
            await UniTask.Yield();
            return true;
#endif
#else
            await UniTask.Yield();
            return true;
#endif
        }
    }
}
